import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/adminView/Home.vue'
import Login from '../views/Login.vue'

import Regist from '../views/Regist.vue'

import User from '../views/adminView/User.vue'
import Chewei from '../views/adminView/Chewei.vue'
import Orders from '../views/adminView/Orders.vue'
import UHome from '@/views/userView/UHome.vue'
import BDMap from '@/views/userView/BDMap.vue'
import UserMap from '@/views/userView/UserMap.vue'
import UOrders from '@/views/userView/UOrders.vue'
import Parking from '@/views/userView/Parking.vue'
import UserInfo from '@/views/userView/userControllerView/UserInfo.vue'
import CarController from '@/views/userView/userControllerView/CarController.vue'
import RentChewei from '@/views/userView/userControllerView/RentChewei.vue'



Vue.use(VueRouter)

const routes = [
  {
    path: '/Home', component: Home,
    children:[
      {
        path: '/Home/User', component: User
      },
      {
        path: '/Home/Chewei', component: Chewei
      },
      {
        path: '/Home/Orders', component: Orders
      },
    ]
  },
  {
    path: '/Login', component: Login
  },
  {
    path: '/UHome', component: UHome,
    children:[
      {
        path: '/UHome/BDMap', component:BDMap
      },
      {
        path: '/UHome/UserMap', component:UserMap,
        children:[
          {
            path: '/UHome/UserMap/UserInfo', component:UserInfo,
          },
          {
            path: '/UHome/UserMap/CarController', component:CarController,
          },
          {
            path: '/UHome/UserMap/RentChewei', component:RentChewei,
          },
        ]
      },
      {
        path: '/UHome/UOrders', component:UOrders
      },
      {
        path: '/UHome/Parking', component:Parking
      },
    ]
  },
  {
    path: '/Regist', component: Regist
  },


]

const router = new VueRouter({
  routes
});

// 设置路由守卫
router.beforeEach((to, from, next) => {
  //  后期开发中，先都放行方便测试
  // next();
  // return;

  //如果目的地是login组件，则直接放行
  if (to.path == "/login") {
    next();
    return;
  }
  if (to.path == "/regist") {
    next();
    return;
  }

  //如果用户没有登录，就不放行
  if (sessionStorage.getItem("u") == null) {
    router.push("/login");
    return;
  }
  //否则就放行到目的地
  next();

});

export default router
